﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using DataControls;

namespace GiaoVien_QuanLy_DeTai
{
    public partial class LUONGGD_Form : Form
    {
        LUONGGD_Data luonggd_Data = new LUONGGD_Data();
        THAMSO thamso = new THAMSO();
        LOPHOC_Data lophoc_Data = new LOPHOC_Data();
        GIANGDAY_Data giangday_Data = new GIANGDAY_Data();
        LUONGGD luonggd = new LUONGGD();
        GIAOVIEN_Data giaovien_Data = new GIAOVIEN_Data();
        public LUONGGD_Form()
        {
            InitializeComponent();
        }

        private void LUONGGD_Form_Load(object sender, EventArgs e)
        {
            txtMaLuongGD.Text = "LGD";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtTienLuongGD.Text = "";
            txtTongTietGD.Text = "";

            cbGiaoVien.Text = "";
            cbLopGD.Text = "";

            cbGiaoVien.Items.Clear();
            cbLopGD.Items.Clear();

            giaovien_Data.giaovien_Combobox(cbGiaoVien);
        }

        private void btnHienThi_Click(object sender, EventArgs e)
        {
            txtMaLuongGD.Text = "LGD";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtTienLuongGD.Text = "";
            txtTongTietGD.Text = "";

            cbGiaoVien.Text = "";
            cbLopGD.Text = "";

            luonggd.hienthi_LuongGD(dtgLuongGD);
            cbGiaoVien.Items.Clear();
            cbLopGD.Items.Clear();

            giaovien_Data.giaovien_Combobox(cbGiaoVien);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaLuongGD.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ma Luong Giang Day");
                txtMaLuongGD.Focus();
                return;
            }
            else if (luonggd_Data.kiemtra_MaLuongGD(txtMaLuongGD.Text.ToString()))
            {
                MessageBox.Show("Ban nhap trung ma");
                txtMaLuongGD.Focus();
                return;
            }
            else if (txtNamHoc.Text == "")
            {
                MessageBox.Show("Ban chua nhap nam hoc");
                txtNamHoc.Focus();
                return;
            }
            else if (txtTongTietGD.Text == "")
            {
                MessageBox.Show("Ban chua co Tiet giang day");
                return;
            }
            else if (txtTienLuongGD.Text == "")
            {
                MessageBox.Show("Ban chua co Tien luong giang day");
                return;
            }
            else if (cbGiaoVien.Text == "")
            {
                MessageBox.Show("Ban chua chon giao vien");
                return;
            }
            else if (nmHocKy.Value < 0 || nmHocKy.Value > 20)
            {
                MessageBox.Show("ban chon hoc ki sai");
                return;
            }
            else
            {
                luonggd.maluonggd = txtMaLuongGD.Text.ToString();
                luonggd.magv = ((ListItem)(cbGiaoVien.SelectedItem)).value;
                luonggd.hocki = (int)nmHocKy.Value;
                luonggd.tongtietgiangday = (float)Convert.ToDouble(txtTongTietGD.Text);
                luonggd.luonggd = (float)Convert.ToDouble(txtTongTietGD.Text);
                try
                {
                    if ((luonggd.namhoc = Convert.ToInt32(txtNamHoc.Text)) < 0)
                    {
                        MessageBox.Show("Ban nhap sai nam hoc");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban nhap sai nam hoc \n" + ex.Message);
                    txtNamHoc.Focus();
                    return;
                }
                if (luonggd_Data.kiemtra_GiaoVien(luonggd))
                {
                    MessageBox.Show("Ban nhap trung thong tin");
                    return;
                }

                luonggd.them_LuongGD(luonggd);
            }

            txtMaLuongGD.Text = "LGD";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtTienLuongGD.Text = "";
            txtTongTietGD.Text = "";

            cbGiaoVien.Text = "";
            cbLopGD.Text = "";

            luonggd.hienthi_LuongGD(dtgLuongGD);
            cbGiaoVien.Items.Clear();
            cbLopGD.Items.Clear();

            giaovien_Data.giaovien_Combobox(cbGiaoVien);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaLuongGD.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ma Luong Giang Day");
                txtMaLuongGD.Focus();
                return;
            }
            else if (txtNamHoc.Text == "")
            {
                MessageBox.Show("Ban chua nhap nam hoc");
                txtNamHoc.Focus();
                return;
            }
            else if (txtTongTietGD.Text == "")
            {
                MessageBox.Show("Ban chua co Tiet giang day");
                return;
            }
            else if (txtTienLuongGD.Text == "")
            {
                MessageBox.Show("Ban chua co Tien luong giang day");
                return;
            }
            else if (cbGiaoVien.Text == "")
            {
                MessageBox.Show("Ban chua chon giao vien");
                return;
            }
            else if (nmHocKy.Value < 0 || nmHocKy.Value > 20)
            {
                MessageBox.Show("ban chon hoc ki sai");
                return;
            }
            else
            {
                luonggd.maluonggd = txtMaLuongGD.Text.ToString();
                luonggd.magv = ((ListItem)(cbGiaoVien.SelectedItem)).value;
                luonggd.hocki = (int)nmHocKy.Value;
                luonggd.tongtietgiangday = (float)Convert.ToDouble(txtTongTietGD.Text);
                luonggd.luonggd = (float)Convert.ToDouble(txtTienLuongGD.Text);
                try
                {
                    if ((luonggd.namhoc = Convert.ToInt32(txtNamHoc.Text)) < 0)
                    {
                        MessageBox.Show("Ban nhap sai nam hoc");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban nhap sai nam hoc \n" + ex.Message);
                    txtNamHoc.Focus();
                    return;
                }
                if (luonggd_Data.kiemtra_GiaoVien(luonggd))
                {
                    MessageBox.Show("Ban nhap trung thong tin");
                    return;
                }
                luonggd.sua_LuongGD(luonggd);
            }


            txtMaLuongGD.Text = "LGD";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtTienLuongGD.Text = "";
            txtTongTietGD.Text = "";

            cbGiaoVien.Text = "";
            cbLopGD.Text = "";

            luonggd.hienthi_LuongGD(dtgLuongGD);
            cbGiaoVien.Items.Clear();
            cbLopGD.Items.Clear();

            giaovien_Data.giaovien_Combobox(cbGiaoVien);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaLuongGD.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ma Luong Giang Day");
                txtMaLuongGD.Focus();
                return;
            }
            else
            {
                luonggd.xoa_LuongGD(txtMaLuongGD.Text.ToString());
            }

            txtMaLuongGD.Text = "LGD";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtTienLuongGD.Text = "";
            txtTongTietGD.Text = "";

            cbGiaoVien.Text = "";
            cbLopGD.Text = "";

            luonggd.hienthi_LuongGD(dtgLuongGD);
            cbGiaoVien.Items.Clear();
            cbLopGD.Items.Clear();

            giaovien_Data.giaovien_Combobox(cbGiaoVien);
        }

        private void cbGiaoVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtNamHoc.Text == "")
            {
                MessageBox.Show("Ban chua nhap nam hoc");
                txtNamHoc.Focus();
                return;
            }
            else if (nmHocKy.Value <= 0 || nmHocKy.Value > 20)
            {
                MessageBox.Show("ban chon hoc ki sai");
                cbGiaoVien.Items.Clear();
                giaovien_Data.giaovien_Combobox(cbGiaoVien);
                return;
            }
            try
            {
                if ((luonggd.namhoc = Convert.ToInt32(txtNamHoc.Text)) < 0)
                {
                    MessageBox.Show("Ban nhap sai nam hoc");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ban nhap sai nam hoc \n" + ex.Message);
                txtNamHoc.Focus();
                txtTongTietGD.Text = "";
                return;
            }
            String magiaovien = ((ListItem)(cbGiaoVien.SelectedItem)).value;
            int hk = (int)nmHocKy.Value;
            int nh = Convert.ToInt16(txtNamHoc.Text);

            giangday_Data.hienthi_GiangDay(dtgLopGD, magiaovien);

            cbLopGD.Items.Clear();
            lophoc_Data.lophoc_GiaoVien(cbLopGD, magiaovien);

            float tongtietday = giangday_Data.tongtietday(magiaovien,hk,nh);
            float tietchunhiem =(float) (lophoc_Data.solop_ChuNhiem(magiaovien) * thamso.tietchuan_ChuNhiem());
            float tongtiet = tongtietday + tietchunhiem;
            txtTongTietGD.Text = tongtiet.ToString();
            float tienTC = thamso.tien_TietChuan();
            float tienluong = tienTC * tongtiet;
            txtTienLuongGD.Text = ((double)tienluong).ToString();

        }

        private void dtgLuongGD_RowEnter(object sender, DataGridViewCellEventArgs e)
        {

            dtgLuongGD.AllowUserToAddRows = false;
            int dong = e.RowIndex;
            txtMaLuongGD.Text = dtgLuongGD.Rows[dong].Cells[0].Value.ToString();
            txtNamHoc.Text = dtgLuongGD.Rows[dong].Cells[3].Value.ToString();
            txtTongTietGD.Text = dtgLuongGD.Rows[dong].Cells[4].Value.ToString();
            txtTienLuongGD.Text = dtgLuongGD.Rows[dong].Cells[5].Value.ToString();
            nmHocKy.Value = (int)dtgLuongGD.Rows[dong].Cells[2].Value;

            String magiaovien = dtgLuongGD.Rows[dong].Cells[1].Value.ToString();
            giangday_Data.layten_GiaoVien(magiaovien, cbGiaoVien);
        }

       

        
      

       

        private void txtMaLuongGD_Click(object sender, EventArgs e)
        {
            txtMaLuongGD.Text = "LGD";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtTienLuongGD.Text = "";
            txtTongTietGD.Text = "";

            cbGiaoVien.Text = "";
            cbLopGD.Text = "";

            cbGiaoVien.Items.Clear();
            cbLopGD.Items.Clear();

            giaovien_Data.giaovien_Combobox(cbGiaoVien);
        }

       
    }
}
